<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .container{
            width: 600px;
            height: 600px;
            border: 10px solid pink;
            border-radius: 10px;
            margin: 0 auto;
            position: relative;
        }
        .container div{
            width: 30%;
            height: 30%;
            background: gray;
            position: absolute;
            font-size: 50px;
            color: white;
            display: flex;
            justify-content: center;
            align-items: center;
            border-radius: 5px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div style="left:2%;top:2%">1</div>
        <div style="left:35%;top:2%">2</div>
        <div style="left:68%;top:2%">3</div>
        <div style="left:2%;top:35%">4</div>
        <div style="left:35%;top:35%">5</div>
        <div style="left:68%;top:35%">6</div>
        <div style="left:2%;top:68%">7</div>
        <div style="left:35%;top:68%">8</div>
        <div style="left:68%;top:68%">9</div>
    </div>
    <script>
        var arr = ['red','blue','yellow','orange','black','pink','gray','purple','green'];
        let divs = document.querySelectorAll(".container div");
        divs.forEach(function(div,key){
            div.style.backgroundColor = arr[key];
        })

        // 实现多个元素的拖拽 
        divs.forEach(function(div){ 
            div.onmousedown = function(e){
                this.style.zIndex = 10;
                var e = e || window.event;
                var x = e.clientX - this.offsetLeft;
                var y = e.clientY - this.offsetTop;
                div.onmousemove = function(e){
                    var e = e || window.event;

                    // 检测拖动的方块和其他方块是否碰撞了；
                    divs.forEach(function(item){
                        // 不要用自己和自己来检测碰撞；
                        if(div!==item){
                            if(
                                Math.abs( div.offsetLeft - item.offsetLeft)<div.offsetWidth
                                && Math.abs(div.offsetTop -  item.offsetTop )<div.offsetHeight
                            ){
                                console.log("有方块和当前拖动方块碰撞了");
                            }

                        }
                    }) 

                    //练习：  拖动之后 方块会自动回位置；
                    // 碰撞的面积  ：宽： A.offsetWidth -  Math.abs(AoffsetLeft - BoffsetLeft) 高：

                    var xx = e.clientX - x;
                    var yy = e.clientY - y;
                    this.style.left = xx + "px";
                    this.style.top = yy + "px";
                }
                document.onmouseup = function(){
                    div.onmousemove = "";
                    div.style.zIndex = 1;
                }
            }
        })


    </script>
</body>
</html>